export const TYPES = {
  GET_EMPLOYEE_LIST: 'employees/getemployeelist',
  GET_EMPLOYEE_LIST_SUCCESS: 'employees/getemployeelistsuccess',
  GET_EMPLOYEE_LIST_FAIL: 'employees/getemployeelistfail',
  DELETE_EMPLOYEE: 'employees/deleteemployee',
  DELETE_EMPLOYEE_SUCCESS: 'employees/deleteemployeesuccess',
  DELETE_EMPLOYEE_FAIL: 'employees/deleteemployeefail',
  ADD_EMPLOYEE: 'employees/addeemployee',
  EDIT_EXISTING_EMPLOYEE: 'employees/editexistingemployee',
  SAVE_EMPLOYEE: 'employees/saveemployee',
  SAVE_EMPLOYEE_SUCCESS: 'employees/saveemployeesuccess',
  SAVE_EMPLOYEE_FAIL: 'employees/saveemployeefail',
  CANCEL_EDIT_EMPLOYEE: 'employees/canceleditemployee',
};

const EmptyEmployee = {
  user_id: '',
  user_no: '',
  status: 1,
  name: '',
  mobile: '',
  pincode: '',
  note: '',
  roles: '',
  group_id: '4',
  group_name: 'Chien testing',
  email: '',
  password: '',
};

const DefaultStore = {
  loading: false,
  saving: false,
  deleting: false,
  employees: [],
  error: {},
  editingEmployee: {},
};

export default function(store = DefaultStore, action) {
  switch (action.type) {
    case TYPES.GET_EMPLOYEE_LIST:
      return {
        ...store,
        loading: true,
        error: {},
      };

    case TYPES.GET_EMPLOYEE_LIST_SUCCESS:
      return {
        ...store,
        loading: false,
        employees: action.employees,
        error: {},
      };

    case TYPES.GET_EMPLOYEE_LIST_FAIL:
      return {
        ...store,
        loading: false,
        error: {
          type: TYPES.GET_EMPLOYEE_LIST_FAIL,
        },
      };

    case TYPES.DELETE_EMPLOYEE:
      return {
        ...store,
        deleting: true,
        error: {},
      };

    case TYPES.DELETE_EMPLOYEE_SUCCESS:
      return {
        ...store,
        deleting: false,
        employees: store.employees.filter(employee => employee.user_id != action.user_id),
        error: {},
      };

    case TYPES.DELETE_EMPLOYEE_FAIL:
      return {
        ...store,
        deleting: false,
        error: {
          type: TYPES.DELETE_EMPLOYEE_FAIL,
        },
      };

    case TYPES.ADD_EMPLOYEE:
      return {
        ...store,
        editingEmployee: EmptyEmployee,
        error: {},
      };

    case TYPES.EDIT_EXISTING_EMPLOYEE:
      return {
        ...store,
        editingEmployee: action.employee,
        error: {},
      };

    case TYPES.SAVE_EMPLOYEE:
      return {
        ...store,
        saving: true,
        error: {},
      };

    case TYPES.SAVE_EMPLOYEE_SUCCESS:
      const employees = (action.is_new) ?
        [action.employee, ...store.employees] :
        store.employees.map(item => {
          if (item.user_id === action.employee.user_id) {
            return { ...item, ...action.employee };
          }

          return item;
        });

      return {
        ...store,
        employees: employees,
        saving: false,
        editingEmployee: {},
        error: {},
      };

    case TYPES.SAVE_EMPLOYEE_FAIL:
      return {
        ...store,
        saving: false,
        error: {
          type: TYPES.SAVE_EMPLOYEE_FAIL,
        },
      };

    case TYPES.CANCEL_EDIT_EMPLOYEE:
      return {
        ...store,
        editingEmployee: {},
        error: {},
      };

    default:
      return store;
  }
};